#!/usr/bin/python3
# -*- coding: utf-8 -*-

from playwright.sync_api import Page

from models.base_page import BasePage
from utils.cache_util import cache_manager


class LoginPage(BasePage):
    def __init__(self, page: Page):
        super().__init__(page)
        self.locator_username_public_cloud = self.page.get_by_role("textbox", name="手机号/邮箱")
        self.locator_password_public_cloud = self.page.get_by_role("textbox", name="请输入密码")
        self.locator_login_button = self.page.get_by_text("登录", exact=True).nth(1)

        self.locator_username_private_cloud = self.page.locator("#normalLogin input[type='text']")
        self.locator_password_private_cloud = self.page.locator("#normalLogin input[type='password']")

    def navigate(self) -> None:
        """导航到登录界面"""
        super().navigate(other_url=self.login_url)

    def fill_username(self, username: str) -> None:
        """输入用户名"""
        if cache_manager.get("public_cloud"):
            self.locator_username_public_cloud.fill(username)
            self.expect.to_have_value(locator=self.locator_username_public_cloud, value=username)
        else:
            self.locator_username_private_cloud.fill(username)
            self.expect.to_have_value(locator=self.locator_username_private_cloud, value=username)

    def fill_password(self, password: str) -> None:
        """输入密码"""
        if cache_manager.get("public_cloud"):
            self.locator_password_public_cloud.fill(password)
            self.expect.to_have_value(locator=self.locator_password_public_cloud, value=password)
        else:
            self.locator_password_private_cloud.fill(password)
            self.expect.to_have_value(locator=self.locator_password_private_cloud, value=password)

    def click_login_button(self) -> None:
        """点击登录按钮"""
        self.locator_login_button.click()
        self.wait_for_load_state(state="load")
